<template>
  <div class="common-layout">
    <el-row class="min-h-10 h-screen">
      <el-col :lg="16" class="flex justify-center items-center bg-blue-400" style="display: flex;">
        <div>
          <div class="font-bold text-6xl text-white">高考数据分析系统</div>
          <div>我们将提供最好的服务</div>
        </div>
      </el-col>
      <el-col :lg="8" class="flex items-center justify-center bg-gray-100" style="display: flex;">
        <div>
          <h2 class="text-center font-bold text-3xl pb-10">注册</h2>
          <!-- 注册类型单选框 -->
          <el-radio-group v-model="registerType" class="mb-5">
            <el-radio label="student">学生注册</el-radio>
            <el-radio label="university">高校注册</el-radio>
            <el-radio label="education">教育管理部门注册</el-radio>
          </el-radio-group>
          
          <el-form ref="registerRef" :model="registerForm" :rules="registerRules" class="register-form">
            <el-form-item prop="username">
              <el-input v-model="registerForm.username" type="text" size="large" auto-complete="off" placeholder="账号" class="h-[48px]">
                <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="registerForm.password" type="password" size="large" auto-complete="off" placeholder="密码" @keyup.enter="handleRegister" class="h-[48px]">
                <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
              </el-input>
            </el-form-item>
            <el-form-item prop="confirmPassword">
              <el-input v-model="registerForm.confirmPassword" type="password" size="large" auto-complete="off" placeholder="确认密码" @keyup.enter="handleRegister" class="h-[48px]">
                <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
              </el-input>
            </el-form-item>
            
            <!-- 仅在学生注册时显示省份下拉框和成绩输入框 -->
            <template v-if="registerType === 'student'">
              <el-form-item prop="province">
                <el-select v-model="registerForm.province" placeholder="请选择省份" class="w-full">
                  <el-option label="北京" value="北京"></el-option>
                  <el-option label="上海" value="上海"></el-option>
                  <!-- 继续列出所有省份 -->
                  <el-option label="国外" value="国外"></el-option>
                </el-select>
              </el-form-item>
              
              <!-- 学生成绩输入框 -->
              <el-form-item v-for="(score, index) in registerForm.scores" :key="index" :prop="'score' + index">
                <el-input v-model="registerForm.scores[index]" type="number" size="large" auto-complete="off" :placeholder="'请输入成绩' + (index + 1)" class="h-[48px]"/>
              </el-form-item>
            </template>

            <el-form-item prop="code" v-if="captchaEnabled">
              <el-input size="large" v-model="registerForm.code" auto-complete="off" placeholder="验证码" style="width: 63%" @keyup.enter="handleRegister " class="h-[48px]">
                <template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon" /></template>
              </el-input>
              <div class="register-code">
                <img :src="codeUrl" @click="getCode" class="login-code-img" />
              </div>
            </el-form-item>
            <el-form-item style="width:100%;">
              <el-button :loading="loading" size="large" type="primary" style="width:100%;" @click.prevent="handleRegister">
                <span v-if="!loading">注册</span>
                <span v-else>注册中...</span>
              </el-button>
            </el-form-item>
            <el-form-item style="width:100%;">
              <router-link to="/login">
                <el-button class="block" style="margin-top: 10px;">返回登录</el-button>
              </router-link>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>
    <!-- 底部 -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2024 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, getCurrentInstance } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessageBox } from 'element-plus';
import { register, getCodeImg } from '@/api/login';

const router = useRouter();
const { proxy } = getCurrentInstance();

const registerType = ref('student'); // 注册类型
const registerForm = reactive({
  username: "",
  password: "",
  confirmPassword: "",
  code: "",
  uuid: "",
  province: "", // 省份选择
  scores: Array(9).fill('') // 成绩输入框
});

const registerRules = {
  username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  confirmPassword: [{ required: true, trigger: "blur", message: "请确认您的密码" }],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }],
  province: [{ required: registerType.value === 'student', trigger: "change", message: "请选择省份" }]
};

const codeUrl = ref("");
const loading = ref(false);
const captchaEnabled = ref(true);

function handleRegister() {
  proxy.$refs.registerRef.validate(valid => {
    if (valid) {
      loading.value = true;
      register(registerForm).then(res => {
        const username = registerForm.username;
        ElMessageBox.alert("<font color='red'>恭喜你，您的账号 " + username + " 注册成功！</font>", "系统提示", {
          dangerouslyUseHTMLString: true,
          type: "success",
        }).then(() => {
          router.push("/login");
        }).catch(() => {});
      }).catch(() => {
        loading.value = false;
        if (captchaEnabled.value) {
          getCode();
        }
      });
    }
  });
}

function getCode() {
  getCodeImg().then(res => {
    captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled;
    if (captchaEnabled.value) {
      codeUrl.value = "data:image/gif;base64," + res.img;
      registerForm.uuid = res.uuid;
    }
  });
}

getCode();
</script>

<style scoped>
.common-layout {
  height: 100%;
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.register-code {
  width: 33%;
  height: 40px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.login-code-img {
  height: 40px;
  padding-left: 12px;
}
</style>
